package com.qiyou.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qiyou.dao.IScenicSpotDao;
import com.qiyou.dao.factory.DaoFactory;
import com.qiyou.entity.ScenicSpot;

/**
 * 加载景点列表
 */
@WebServlet(name = "ScenicSpotList", urlPatterns = { "/ScenicSpotList" })
public class ScenicSpotListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String sort = request.getParameter("sort");
		
		IScenicSpotDao spotDao = DaoFactory.getScenicSpotDao();
		List<ScenicSpot> spots = null;

		try {
			if ("nsspt".equalsIgnoreCase(sort)) {
				String sql = "order by scsID desc limit 25";
				spots = spotDao.getScenicSpots(new Object[]{sql});
			}else {
				String sql = "order by scsNums desc limit 25";
				spots = spotDao.getScenicSpots(new Object[]{sql});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if (null != spots) {
			request.setAttribute("spots", spots);
			request.getRequestDispatcher("sspotlist.jsp").forward(request, response);
		}else {
			response.getWriter().print("<script type='text/javascript'>alert('没能获取到任何参数！');history.go(-1);</script>");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
